﻿using CGT.API.API;
using CGT.API.Common;
using CGT.API.Models;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CGT.API.Controllers
{
    /// <summary>
    /// 邮寄地址管理
    /// </summary>
    public class MailAddressController : BaseApiController
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> Add(MailAddressModel model)
        {
            if (!ValidateHelper.IsMobileNumber(model.MailUserMobile))
            {
                ResultData<int>.Error("用户手机号码不正确");
            }
            int id = MailAddressOperate.Insert(new OF.Component.Model.MailAddressInfo()
            {
                MailAddress = model.MailAddress,
                MailAreaID = model.MailAreaID,
                MailUserMobile = model.MailUserMobile,
                MailUserName = model.MailUserName,
                UserID = model.UserID,
                IsDefault = model.IsDefault
            });
            return ResultData<int>.Success(id);
            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResponseCommonOfInt32 rt = usrClient.MailAddress_AddAsync(new RequestMailAddressAdd()
            //{
            //    MailAddress = model.MailAddress,
            //    MailAreaID = model.MailAreaID,
            //    MailUserMobile = model.MailUserMobile,
            //    MailUserName = model.MailUserName,
            //    UserID = model.UserID,
            //    IsDefault = model.IsDefault
            //}).Result;
            //if (rt.Status == 0)
            //{
            //    return ResultData<int>.Success(rt.Data.ModelID.Value);
            //}
            //return ResultData<int>.Error(rt.Message);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> Update(MailAddressModel model)
        {
            var oHasThisAddress = MailAddressOperate.GetByID(model.MailAddressID);
            if (oHasThisAddress == null)
            {
                return ResultData<int>.Error("邮寄地址信息不存在");
            }
            if (!ValidateHelper.IsMobileNumber(model.MailUserMobile))
            {
                ResultData<int>.Error("用户手机号码不正确");
            }
           MailAddressOperate.Update(new OF.Component.Model.MailAddressInfo()
            {
               MailAddressID=model.MailAddressID,
                MailAddress = model.MailAddress,
                MailAreaID = model.MailAreaID,
                MailUserMobile = model.MailUserMobile,
                MailUserName = model.MailUserName,
                UserID = model.UserID,
                IsDefault = model.IsDefault
            });
            return ResultData<int>.Success(model.MailAddressID);
            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResponseCommonOfInt32 rt = usrClient.MailAddress_UpdateAsync(new RequestMailAddressUpdate()
            //{
            //    MailAddressID = model.MailAddressID,
            //    MailAddress = model.MailAddress,
            //    MailAreaID = model.MailAreaID,
            //    MailUserMobile = model.MailUserMobile,
            //    MailUserName = model.MailUserName,
            //    UserID = model.UserID,
            //    IsDefault = model.IsDefault
            //}).Result;
            //if (rt.Status == 0)
            //{
            //    return ResultData<int>.Success(rt.Data.ModelID.Value);
            //}
            //return ResultData<int>.Error(rt.Message);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="conditon"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultPageData<List<MailAddressModel>> GetList([FromUri]MaillAddressConditon conditon)
        {
            ResultPageData<List<MailAddressModel>> rt = new ResultPageData<List<MailAddressModel>>();
            rt.data = new List<MailAddressModel>();
            int total;
            var list = MailAddressOperate.SearchList(new OF.Component.DTO.MailAddressConditon()
            {
                page = conditon.PageIndex,
                rows = conditon.PageSize,
                UserID = conditon.UserID
            }, out total);
            rt.total = total;

            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResponseMailAddressGetList result = usrClient.MailAddress_GetListAsync(conditon.UserID, conditon.PageSize, conditon.PageIndex).Result;
            //if (result.Status == 0)
            //{
            //    List<MailAddressModel> data = new List<MailAddressModel>();
            foreach (var item in list)
            {
                rt.data.Add(new MailAddressModel()
                {
                    MailAddress = item.MailAddress,
                    MailAddressID = item.MailAddressID,
                    MailAreaAddress = item.AreaName,
                    MailAreaID = item.MailAreaID,
                    MailUserMobile = item.MailUserMobile,
                    MailUserName = item.MailUserName,
                    UserID = item.UserID,
                    IsDefault = item.IsDefault
                });
            }
            //    rt.data = data;
            //    rt.total = result.Data.Total.Value;
            //}
            //else
            //{
            //    rt.status = result.Status.Value;
            //    rt.message = result.Message;
            //}
            return rt;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<int> Delete(DeleteModel model)
        {
            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResponseCommonOfInt32 result = usrClient.MailAddress_RemoveAsync(EConvert.ToInt(model.Id)).Result;
            //if (result.Status != 0)
            //{
            //    return ResultData<int>.Error("删除失败");
            //}
            MailAddressOperate.Delete(EConvert.ToInt(model.Id));
            return ResultData<int>.Success();
        }

        /// <summary>
        /// 获取默认邮寄地址
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<MailAddressModel> GetDefault(long userId)
        {
            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResponseMailAddressGet result = usrClient.MailAddress_GetDefaultAsync(userId).Result;
            //if (result.Status != 0)
            //{
            //    return ResultData<MailAddressModel>.Error(result.Message);
            //}
            var oExist = MailAddressOperate.GetDefault(userId);
            if (oExist == null)
            {
                return ResultData<MailAddressModel>.Success();
            }

            MailAddressModel model = new MailAddressModel()
            {
                MailAddress = oExist.MailAddress,
                MailAddressID = oExist.MailAddressID,
                MailAreaID = oExist.MailAreaID,
                MailUserMobile = oExist.MailUserMobile,
                MailUserName = oExist.MailUserName,
                UserID = oExist.UserID,
                IsDefault = oExist.IsDefault
            };


            BaseLocation_AllInfo baseInfo = BaseLocation_AllOperate.GetByID(oExist.MailAddressID);
            if (baseInfo != null)
            {
                model.MailAreaAddress = string.Format("{0}{1}{2}", baseInfo.pName, baseInfo.cName, baseInfo.locationName);
            }

            return ResultData<MailAddressModel>.Success(model);


        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="addressId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<MailAddressModel> GetDetail(int addressId)
        {
            //ResultData<MailAddressModel> rt = new ResultData<MailAddressModel>();
            //UserCenterClient usrClient = new UserCenterClient();
            //ResultDataOfResponseMailAddressGet result = usrClient.MailAddress_GetAsync(addressId).Result;
            //if (result.Status == 0)
            //{
            //    MailAddressModel model = new MailAddressModel()
            //    {
            //        MailAddress = result.Data.MailAddress,
            //        MailAddressID = addressId,
            //        MailAreaAddress = result.Data.AreaName,
            //        MailAreaID = result.Data.MailAreaID.Value,
            //        MailUserMobile = result.Data.MailUserMobile,
            //        MailUserName = result.Data.MailUserName,
            //        UserID = result.Data.UserID.Value,
            //        IsDefault = result.Data.IsDefault.Value
            //    };
            //    rt.data = model;
            //}
            //else
            //{
            //    rt.message = result.Message;
            //    rt.status = result.Status.Value;
            //}
            //return rt;
            var oExist = MailAddressOperate.GetByID(addressId);
            if (oExist == null)
            {
                return ResultData<MailAddressModel>.Success();
            }

            MailAddressModel model = new MailAddressModel()
            {
                MailAddress = oExist.MailAddress,
                MailAddressID = oExist.MailAddressID,
                MailAreaID = oExist.MailAreaID,
                MailUserMobile = oExist.MailUserMobile,
                MailUserName = oExist.MailUserName,
                UserID = oExist.UserID,
                IsDefault = oExist.IsDefault
            };


            BaseLocation_AllInfo baseInfo = BaseLocation_AllOperate.GetByID(oExist.MailAddressID);
            if (baseInfo != null)
            {
                model.MailAreaAddress = string.Format("{0}{1}{2}", baseInfo.pName, baseInfo.cName, baseInfo.locationName);
            }

            return ResultData<MailAddressModel>.Success(model);
        }
    }
}
